package com.mmall.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mmall.Exception.ParamException;
import com.mmall.dao.SysDeptMapper;

import com.mmall.model.SysDept;
import com.mmall.param.Deptparam;
import com.mmall.util.BeanBalidator;
import com.mmall.util.LevelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by pc on 2019/3/23.
 */
@Service
public class SysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    public void save (Deptparam param){
        BeanBalidator.check(param);
        if(checkExist(param.getParentId(),param.getName(),param.getId())){
            throw new ParamException("同一层下存在相同名称的部门");
        }
        SysDept sysDept = new SysDept();
        sysDept.setName(param.getName());
        sysDept.setParentId(param.getParentId());
        sysDept.setSeq(param.getSeq());
        sysDept.setRemark(param.getRemark());
        sysDept.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()),param.getParentId()));
        sysDept.setOperator("system");
        sysDept.setOperateIp("127.0.0.1");
        sysDept.setOperateTime(new Date());
        sysDeptMapper.insertSelective(sysDept);
    }

    private boolean checkExist(Integer parentId,String name,Integer deptId){
        return sysDeptMapper.countByNameAndparentId(parentId,name,deptId) > 0;
    }

    private String getLevel(Integer deptId){
        SysDept sysDept = sysDeptMapper.selectByPrimaryKey(deptId);
        if(sysDept == null){
            return null;
        }
        return sysDept.getLevel();
    }

    public void updte(Deptparam param) {
        BeanBalidator.check(param);
        if(checkExist(param.getParentId(),param.getName(),param.getId())){
            throw new ParamException("同一层下存在相同名称的部门");
        }
        SysDept before = sysDeptMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待跟新的部门不存在");
        if(checkExist(param.getParentId(),param.getName(),param.getId())){
            throw new ParamException("同一层下存在相同名称的部门");
        }
        SysDept after = new SysDept();
        after.setId(param.getId());
        after.setName(param.getName());
        after.setParentId(param.getParentId());
        after.setSeq(param.getSeq());
        after.setRemark(param.getRemark());
        after.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()),param.getParentId()));
        after.setOperator("system--update");
        after.setOperateIp("127.0.0.1");
        after.setOperateTime(new Date());
        updateWithChild(before,after);
    }

    @Transactional
    private void updateWithChild(SysDept before,SysDept after){

        String newLevelPrefix = after.getLevel();
        String oldLevelPrefix = before.getLevel();
        List<SysDept> deptList = Lists.newArrayList();
        if(!newLevelPrefix.equals(oldLevelPrefix)){ //部门级别被改变
             deptList = sysDeptMapper.getChildDeptListByLevel(oldLevelPrefix,before.getId());
            if(CollectionUtils.isNotEmpty(deptList)){
                for(SysDept dept:deptList){
                    String level = dept.getLevel();
                    //如果oldLevel是开头的  那么就截取掉 换newLevel

                    if(level.indexOf(oldLevelPrefix) == 0){
                        if(deptList.size() == 1){
                            level = newLevelPrefix + level.substring(oldLevelPrefix.length());
                        }else{
                            level = newLevelPrefix;
                        }

                        dept.setLevel(level);
                    }
                }
                //批量跟新子级
                sysDeptMapper.batchUpdateLevel(deptList);
            }
        }
        if(deptList.size() == 0){
            sysDeptMapper.updateByPrimaryKeySelective(after);
        }

    }
}
